package com.xxxx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.pojo.*;
import com.xxxx.service.IMenuRoleService;
import com.xxxx.service.IMenuService;
import com.xxxx.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: yun2
 * @ClassName PermissController
 * @description:
 * @author: ZhouBin
 * @create: 2022-05-24 10:01
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/system/basic/permiss")
public class PermissController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuRoleService menuRoleService;


    @ApiOperation(value = "获取角色信息")
    @GetMapping(value = "/")
    public List<Role> getAllRoles(){
        return roleService.list();
    }

    @ApiOperation(value = "增加角色")
    @PostMapping(value = "/")
    public RespBean addRole(@RequestBody Role role){
        if (!role.getName().startsWith("ROLE_")){
            role.setName("ROLe_"+role.getName());
        }
        if (roleService.save(role)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }
    @ApiOperation(value = "更新角色")
    @PutMapping(value = "/")
    public RespBean updateRole(@RequestBody Role role){
        if (roleService.updateById(role)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }
    @ApiOperation(value = "删除角色")
    @DeleteMapping(value = "/role/{rid}")
    public RespBean deleteRole(@PathVariable Integer rid){
        if (roleService.removeById(rid)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "角色查询菜单")
    @GetMapping(value = "/menus")
    public List<Menu> getAllMenus(){
        return menuService.getAllMenus();
    }

    @ApiOperation(value = "用角色id查询菜单id")
    @GetMapping(value = "/mid/{rid}")
    public List<Integer> getMidId(@PathVariable Integer rid){
        return menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid",rid)).stream().map(MenuRole::getMid).collect(Collectors.toList());

    }

    @ApiOperation(value = "更新角色菜单")
    @PutMapping(value = "/")
    public RespBean updateMenuRole(Integer rid,Integer[] mids){
        return menuRoleService.updateMenuRole(rid,mids);
    }


}
